package org.pizza.rocket.autoconfigure;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.OrderProducerBean;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import org.pizza.rocket.core.listener.RocketMessageListenerRegister;
import org.pizza.rocket.core.producer.RocketMQProducer;
import org.pizza.rocket.core.producer.TransactionProducerRegister;
import org.pizza.rocket.core.properties.RocketMqProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.env.Environment;
import org.springframework.core.env.StandardEnvironment;

import javax.annotation.PostConstruct;
import java.util.Properties;

/**
 * @author kynb_club
 * email kynb_club@163.com
 * @since 2020/11/23 5:42 下午
 */
@Configuration
@ConditionalOnClass({ObjectMapper.class})
@Import({JacksonFallbackConfiguration.class})
@EnableConfigurationProperties(RocketMqProperties.class)
@AutoConfigureAfter(JacksonAutoConfiguration.class)
public class RocketMQAutoConfiguration {
    private static final Logger log = LoggerFactory.getLogger(RocketMQAutoConfiguration.class);

    @Bean
    public RocketMessageListenerRegister rocketMessageListenerRegister(StandardEnvironment environment, ObjectMapper objectMapper) {
        return new RocketMessageListenerRegister(environment, objectMapper);
    }

    @Autowired
    private Environment environment;

    @PostConstruct
    public void checkProperties() {
        String nameServer = environment.getProperty("rocketmq.name-srv-addr", String.class);
        if (log.isDebugEnabled()) {
            log.debug("rocketmq.name-srv-addr = {}", nameServer);
        }
        if (nameServer == null) {
            log.error("The necessary spring property 'rocketmq.name-srv-addr' is not defined, all rockertmq beans creation are skipped!");
        }
    }

    @Bean
    @ConditionalOnMissingBean(RocketMQProducer.class)
    public RocketMQProducer rocketMQProducer(ObjectMapper objectMapper, RocketMqProperties rocketMqProperties) {
        RocketMQProducer rocketMQProducer = new RocketMQProducer();
        rocketMQProducer.setObjectMapper(objectMapper);

        Properties properties = new Properties();
        properties.setProperty(PropertyKeyConst.AccessKey, environment.resolvePlaceholders(rocketMqProperties.getAccessKey()));
        properties.setProperty(PropertyKeyConst.SecretKey, environment.resolvePlaceholders(rocketMqProperties.getSecretKey()));
        properties.setProperty(PropertyKeyConst.NAMESRV_ADDR, environment.resolvePlaceholders(rocketMqProperties.getNameSrvAddr()));
        //普通生产者
        ProducerBean defaultProducer = new ProducerBean();
        defaultProducer.setProperties(properties);
        rocketMQProducer.setDefaultProducer(defaultProducer);
        //顺序生产者
        OrderProducerBean orderProducer = new OrderProducerBean();
        orderProducer.setProperties(properties);
        rocketMQProducer.setOrderProducer(orderProducer);
        rocketMQProducer.setRocketProperties(rocketMqProperties);
        return rocketMQProducer;
    }

    @Bean
    public TransactionProducerRegister transactionProducerRegister(StandardEnvironment environment,RocketMQProducer rocketMQProducer ) {
        return new TransactionProducerRegister(environment, rocketMQProducer);
    }


}
